package io.wdb.modules.generator.generator.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.wdb.common.utils.ConstantConfig;
import io.wdb.modules.generator.generator.dao.AccountCashDao;
import io.wdb.modules.generator.generator.dao.AccountCashLogDao;
import io.wdb.modules.generator.generator.entity.AccountCashEntity;
import io.wdb.modules.generator.generator.entity.AccountCashLogEntity;
import io.wdb.modules.generator.generator.service.AccountCashService;

@Service("accountCashService")
public class AccountCashServiceImpl implements AccountCashService {
	@Autowired
	private AccountCashDao accountCashDao;
	@Autowired
	private AccountCashLogDao accountCashLogDao;
	
	@Override
	public AccountCashEntity queryObject(Long id){
		return accountCashDao.queryObject(id);
	}
	
	@Override
	public List<AccountCashEntity> queryList(Map<String, Object> map){
		return accountCashDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return accountCashDao.queryTotal(map);
	}
	
	@Override
	public void save(AccountCashEntity accountCash){
		accountCashDao.save(accountCash);
	}
	
	@Override
	public void update(AccountCashEntity accountCash){
		
		AccountCashEntity accountCashEntity = accountCashDao.queryObject(accountCash.getId());
		//变更的账户余额
		BigDecimal changValue = accountCash.getCashAmount().subtract(accountCashEntity.getCashAmount() == null ? new BigDecimal(0):accountCashEntity.getCashAmount());
		//押金变更
		BigDecimal depositChangeValue = accountCash.getDepositAmount().subtract(accountCashEntity.getDepositAmount() == null ? new BigDecimal(0):accountCashEntity.getDepositAmount());
		
		AccountCashLogEntity accountCashLog = new AccountCashLogEntity();
		accountCashLog.setAccountCashId(accountCash.getId());
		accountCashLog.setChargeType(ConstantConfig.ACCOUNT_CASH_LOG_CHARGETYPE_30);
		accountCashLog.setCreateTime(new Date());
		accountCashLog.setCurrency(ConstantConfig.CNY);
		accountCashLog.setOperType(ConstantConfig.ACCOUNT_CASH_LOG_ORDERTYPE_10);
		
		if (changValue.intValue() != 0) {
			accountCashLog.setChangeAmount(changValue);
			accountCashLogDao.save(accountCashLog);
		}
		
		if(depositChangeValue.intValue() != 0) {
			accountCashLog.setChangeAmount(depositChangeValue);
		}
		
		accountCash.setModifyTime(new Date());
		accountCashDao.update(accountCash);
		
	}
	
	@Override
	public void delete(Long id){
		accountCashDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		accountCashDao.deleteBatch(ids);
	}
	
}
